﻿function dateFormats(value) {
    if (null != value) {
        return Ext.Date.format(new Date(value), 'Y-m-d');
    } else {
        return null;
    }
}

function GetDateStr(AddDayCount) {
    var dd = new Date();
    dd.setDate(dd.getDate() + AddDayCount);//获取AddDayCount天后的日期
    var y = dd.getFullYear();
    var m = dd.getMonth() + 1;//获取当前月份的日期
    var d = dd.getDate();
    return y + "-" + m + "-" + d;
}
Ext.define("PSI.InConsignation.Reseraction", {

    createUI: function () {
        var me = this;
        this.panel = Ext.create("Ext.panel.Panel", {

            border: 0,
            layout: "border",
            title: "进口预约审核",
            items: [
                {
                    region: 'west',
                    layout: "fit",
                    width: 300,
                    xtype: "panel",
                    border: 0,
                    split: true,
                    items: [
           
                        me.getalladdress()
                    ]
                }, {
                    region: "center",
                    layout: "border",
                    items: [
                         {
                             region: 'north',
                             layout: "fit",
                             height: "50%",
                             border: 0,
                             xtype: "panel",
                             items: [me.getreserationminxi()]
                         }, {
                             region: 'south',
                             id: 'sou',
                             layout: "fit",
                             height: "50%",
                             border: 0,
                             xtype: "panel",
                             items: [me.getresgrid()]

                         }
                    ]
                }
            ]
        })
    },

    getMainPanel: function () {
        return this.panel;
    },

    getreserationminxi: function () {
        var me = this;
        Ext.define("moldes", {
            extend: "Ext.data.Model",
            fields: ["id", "blNumber", "cntOwner", "cntType", "cntNumber", "address", "goodsOwner",
                "goodsOwnerTel", "unloadMan", "unloadManTel",
                 "memo", "goods", "status", "goodsOwner", {
                     name: "xhsj", convert: function (value, re) {
                         var hh = re.raw.inconsig;
                         return hh.deliverTime;
                     }
                 }]
        });

        var storef = Ext.create("Ext.data.Store", {
            model: "moldes",
            storeId: 'orderlist',
            proxy: {
                type: 'ajax',
                url: '/InConsignation/GetimportRes',
                extraParams:{date:"今天"},
                reader: {
                    type: 'json',
                    root: 'Data'
                }
            },
            autoLoad: true
        })
        return Ext.create("Ext.grid.Panel", {
            viewConfig: { loadingText: "数据加载中...." },
            store: storef,
            id: "importdetile",
            columnLines: true,
            columns: [
               Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
                { header: '', dataIndex: 'id', hidden: true },
                { header: "状态", dataIndex: "status", menuDisabled: true },
                    { header: "运单号", dataIndex: "blNumber", menuDisabled: true },
                    { header: "箱属", dataIndex: "cntOwner", menuDisabled: true },
                    { header: "箱型", dataIndex: "cntType", menuDisabled: true },
                    { header: "箱号/封号", dataIndex: "cntNumber", menuDisabled: true, width: 250 },
                    { header: "门点", dataIndex: "address", menuDisabled: true },
                    { header: "货名", dataIndex: "goods", menuDisabled: true },
                    { header: "货主", dataIndex: "goodsOwner", menuDisabled: true },
                    { header: "货主电话", dataIndex: "goodsOwnerTel", menuDisabled: true },
                    { header: "卸货联系人", dataIndex: "unloadMan", menuDisabled: true },
                    { header: "卸货联系人电话", dataIndex: "unloadManTel", menuDisabled: true },
                    { header: "备注", dataIndex: "memo", menuDisabled: true },
                    {
                        header: '送货时限', dataIndex: 'xhsj', renderer: function (v) {
                            if (v.substring(0, 1) == "0") 
                            {
                                return "";
                            }   
                            else 
                            {
                                return v;
                            }
                        }
                    }
            ],
            renderTo: Ext.getBody(),
            listeners: {
                select: function (view, record, item, index, e, eOpts) {
                    var value = record.raw.id;
                    var resgrid = Ext.getCmp("resgrid");
                    var el = resgrid.getEl();
                    el.mask("加载数据中....");
                    var store = resgrid.getStore();
                    store.setProxy({
                        type: 'ajax',
                        url: '/InConsignation/GetResListbyOrderID',
                        actionMethods: { read: 'POST' },

                        extraParams: {
                            orderID: value
                        },
                        reader: {
                            type: 'json',
                            root: 'data'
                        }
                    });
                    store.load({
                        scope: this,
                        callback: function (records, operation, success) {
                            if (success) {
                                el.unmask();
                            }
                        }
                    });
                }
            }

        })

    },

    getresgrid: function () {
        var me = this;
        var mm = Ext.define("modles", {
            extend: "Ext.data.Model",
            fields: ["id", "v_orderID", "v_carnumber", "v_owner", "v_owner_tel", "f_owner_price", "v_driver", "v_driver_tel", "d_resver_time", "Lastdt", "i_isselect"]
        });

        return Ext.create("Ext.grid.Panel", {
            viewConfig: {
                enableTextSelection: true, loadMask: false,
                getRowClass: function (record, rowIndex, rowParams, store) {
                    if (record.get("i_isselect") == '1') {
                        return "row-f";
                    }
                }
            },
            id: "resgrid",
            tbar: [
                "-",
                { text: '审核预约', handler: me.passReseration, iconCls: "psi-button-add", scope: me }

            ],
            store: Ext.create("Ext.data.Store", {
                autoLoad: false,
                storeId: 'Weixingridstore',
                model: mm,
                sorters: [{
                    property: 'f_owner_price',
                    direction: 'ASC'
                }],
                data: []
         
            }),
            columnLines: true,
            columns: [
               Ext.create("Ext.grid.RowNumberer", { text: "", width: 30 }),
               { header: '', dataIndex: 'id', hidden: true },
               { header: '订单ID', dataIndex: 'v_orderID', hidden: true },
               { header: '车主', dataIndex: 'v_owner' },
               { header: '车主电话', dataIndex: 'v_owner_tel' },
               { header: '车牌号', dataIndex: 'v_carnumber' },
               { header: '司机', dataIndex: 'v_driver' },
               { header: '司机电话', dataIndex: 'v_driver_tel' },
               { header: '车主报价', dataIndex: 'f_owner_price' },
               {
                   header: '预约时间', dataIndex: 'd_resver_time',
                   renderer: function (value) {
                       var str = value.substring(0, 10);
                       return str;
                   }
               },
               {
                   header: '拖车最晚提箱时间', dataIndex: 'Lastdt', renderer: function (v) {
                       var value = v.replace('T', ' ');
                       value=value.substring(0,value.length-3);
                       return value;
                   },
                   width:130
               }
            ],
            selModel: Ext.create("Ext.selection.CheckboxModel"),
            renderTo: Ext.getBody(),
            listeners: {
                itemclick: function (view, record, item, index, e, eOpts) {

                }
            }

        })

    },

    passReseration: function () {
        var me = this;
        var resgrid = Ext.getCmp("resgrid");
        var items = resgrid.getSelectionModel().getSelection();
        if (!items || items.length == 0) {
            PSI.MsgBox.showInfo("没有选择预约订单！");
            return;
        }

        ID = items[0].getData().id;
        parentID = items[0].getData().v_orderID;
        var items = resgrid.getSelectionModel().getSelection();
        if (items == null || items.length != 1) {
            PSI.MsgBox.showInfo("请选择一条要操作的预约订单！");
            return;
        }
        PSI.MsgBox.confirm("确定执行操作！", function () {
            Ext.Ajax.request({
                url: "/InConsignation/ResDecision",
                method: "POST",
                params: { id: ID },
                callback: function (options, success, response) {
                    if (success) {
                        var data = Ext.JSON.decode(response.responseText);
                        if (data == "") {
                            PSI.MsgBox.showInfo("审核通过！", function () {
                                var incomgrid = Ext.getCmp("importdetile");
                                var store = incomgrid.getStore();
                                store.load();

                                var num = store.find("id", parentID);
                                var record = store.getAt(num);
                                incomgrid.getSelectionModel().clearSelections()
                                incomgrid.getSelectionModel().select(record);
                            });
                        }
                        else {
                            PSI.MsgBox.showInfo(data);
                        }

                    }
                }
            });
        })


    },

    exportDetail: function () {
        var address = Ext.getCmp("address").value;
        var strDT = Ext.getCmp("editDTstr").value;
        var endDT = Ext.getCmp("editDTend").value;
        var param = {
            adr: address,
            sdt: dateFormats(strDT),
            edt: dateFormats(endDT)
        }
        Ext.Ajax.request({
            url: "/InConsignation/ImportResViewQuery",
            method: "post",
            params: param,
            callback: function (options, suc, response) {
                if (suc) {
                    var data = Ext.JSON.decode(response.responseText);
                    var incomgrid = Ext.getCmp("importdetile");
                    var store = incomgrid.getStore();
                    store.loadData(data.Data);

                    var com = Ext.getCmp("resgrid");
                    store = com.getStore();
                    store.loadData({});
                }
            }
        });
    },

    onClearQC: function () {
        Ext.getCmp("address").setValue(null);
        Ext.getCmp("editDTstr").setValue(null);
        Ext.getCmp("editDTend").setValue(null);
    },

    getalladdress: function () {
        var mm = Ext.define("address.model", {
            extend: "Ext.data.Model",
            fields: ['id', 'name']
        })
        var adstore = Ext.create('Ext.data.Store', {
            model: mm,
            proxy: {
                type: 'ajax',
                url: '/Address/GetimportListDataStore',
                actionMethods: { read: 'POST' },
                reader: {
                    type: 'json',
                    root: 'dat'
                },
                extraParams: { date: "今天" }
            },
            autoLoad: true,
            listeners: {
                load: function (w,r,s,o) {
                  
                }
            }
        })
       
        return Ext.create("Ext.grid.Panel", {
            tbar: [
                    Ext.create('Ext.form.field.ComboBox', {
                        fieldLabel: '日期',
                        id: 'dt',
                        displayField: 'name',
                        labelWidth: 40,
                        labelAlign:"center",
                        store: Ext.create("Ext.data.Store", {
                            fields: ["name"],
                            data: [{ "id": "one", "name": "今天" + GetDateStr(0) }, { "id": "zone", "name": "昨天" + GetDateStr(-1) }, { "id": "two", "name": "明天" + GetDateStr(1) }]
                        }),
                        queryMode: 'local',
                       // valuefield: 'name',
                        value: "今天" + GetDateStr(0),
                        editable: false,//不可编辑
                        listeners: {
                            change: function (me, obj, old, open) {
                                adstore.load({
                                    params: {
                                        date: obj
                                    }
                                });
                                var importdeatilestore = Ext.data.StoreManager.lookup("Weixingridstore");
                                importdeatilestore.loadData({});
                                var mm = Ext.data.StoreManager.lookup("orderlist");
                                mm.setProxy({
                                    type: 'ajax',
                                    url: '/InConsignation/GetimportRes',
                                    reader: {
                                        type: 'json',
                                        root: 'Data'
                                    },
                                    extraParams: { date: obj }
                                })
                                mm.load()   
                            }
                        }

                    })
            ],
            viewConfig: { loadingText: "数据加载中...." },
            id:"address",
            store: adstore,
            columnLines: true,
            autoheight:true,
            columns: [
               Ext.create("Ext.grid.RowNumberer", { text: "序号", width: "14%", align: "center" }),
               { header: '', dataIndex: 'id', hidden: true },
               { header: '门点', dataIndex: "name", width: "85%" }
            ],
            renderTo: Ext.getBody(),
            listeners: {
                itemclick: function (view, record, item, index, e, eOpts) {
                    var address = record.raw.name;
                    var date = Ext.getCmp("dt").value;
                    var getgrid = Ext.getCmp("importdetile")
                    var store = getgrid.getStore();
                    store.setProxy({
                        type: 'ajax',
                        url: '/InConsignation/ImportResViewQuery',
                        actionMethods: { read: 'POST' },
                        extraParams: {
                            adr: address,
                            date: date
                        },
                        reader: {
                            type: 'json',
                            root: 'Data'
                        }
                    })
                    store.load();
                    var importdeatilestore = Ext.data.StoreManager.lookup("Weixingridstore");
                    importdeatilestore.loadData({});
                }
            }
        });
    }
})